%    9372/30 (312.4, 312.4) sec -> DUR 5:12.6; END 5:12.6
%   15865/30 (528.8, 841.2) sec -> DUR 8:48.8; END 14:1.3
%   11576/30 (385.9, 1227.1)sec -> DUR 6:25.9; END 20:27.2
% to pups 
% ---relative--   --globa time(sec)---
% [00:38-01:34;    38 - 95
%  01:36-02:56;    96 - 176
%  03:01-03:21;   181 - 201
%  03:51-04:20;   231 - 260
%  04:27-04:29;   267 - 269
%  04:33-04:35;   273 - 275
% 
% to male
% ---relative--   --globa time(sec)---
% [00:38-00:55;    350.4 - 367.4
% 00:58-01:04;     370.4 - 376.4
% 01:25-01:27;     397.4 - 399.4
% 01:34-02:00;     406.4 - 432.4
% 02:13-02:16;     445.4 - 448.4
% 02:35-02:37;     467.4 - 469.4
% 02:41-02:54;     473.4 - 486.4
% 03:08-03:11;     500.4 - 503.4
% 03:25-03:28;     517.4 - 520.4
% 04:27-04:36;     579.4 - 688.4
% 04:50-05:04;     602.4 - 616.4
% 05:51-06:01;     663.4 - 676.4
% 06:40-06:47;     712.4 - 719.4
% 06:55-07:07;     727.4 - 739.4
% 07:53-08:12      785.4 - 804.4
% 
% to female       
% ---relative--   --globa time(sec)---
% [00:34-01:17;    875.2 - 918.2
%  01:30-01:40;    931.2 - 941.2
%  01:48-03:02;    949.2 - 1023.2
%  03:19-03:29;   1040.2 - 1050.2
%  03:54-04:06;   1075.2 - 1087.2
%  04:17-04:27;   1098.2 - 1108.2
%  04:45-04:49;   1126.2 - 1130.2
%  05:05-05:15;   1146.2 - 1156.2
%  05:20-05:30;   1161.2 - 1171.2
%  05:47-05:57];  1188.2 - 1198.2
events = cell(3,1);
events{1} = [38  95
            96  176
            181  201
            231  260
            267  269
            273  275];
events{2} =[350.4  367.4
            370.4  376.4
            397.4  399.4
            406.4  432.4
            445.4  448.4
            467.4  469.4
            473.4  486.4
            500.4  503.4
            517.4  520.4
            579.4  688.4
            602.4  616.4
            663.4  676.4
            712.4  719.4
            727.4  739.4
            785.4  804.4];
events{3} = [875.2  918.2
            931.2  941.2
            949.2  1023.2
            1040.2  1050.2
            1075.2  1087.2
            1098.2  1108.2
            1126.2  1130.2
            1146.2  1156.2
            1161.2  1171.2
            1188.2  1198.2];
%% load data %%
Fs = 5; %sample rate of final calcium data.
[f, p] = uigetfile('*_results.mat');
if f==0; return; end
results_mat = [p, f];
checkFileContent(results_mat, {'results'});
MAT = load(results_mat);
neuron = struct2neuron(MAT.results);

%% calculate- time line
data = neuron.C_raw; %n_neu x time
[n_neu, len] = size(neuron.C_raw);
[A_m, A_std, A_sem] = median_std_sem(data, 2);
data_nomedian = bsxfun(@(x, y)x-y, data, A_m);
data_z = bsxfun(@(x, y)x./y, data_nomedian, A_std);

d = data_z;

%% aligh to event %%
events_now = events{3};
alightoevent_bgend(d, events_now, Fs);

%%
%d2 = d(.....)

%% 
figure()
xoff = [0, 312.4, 841.2];
for i=1:3
    subplot(1,3,i); hold on;
    xoff_now = xoff(i);
    imagesc(d, 'xdata', [0, 1227.1]-xoff_now)
    barline(events{1}(:,1)-xoff_now, [-50 0], 'r', 'linewidth', 2)
    barline(events{2}(:,1)-xoff_now, [-50 0], 'g', 'linewidth', 2)
    barline(events{3}(:,1)-xoff_now, [-50 0], 'b', 'linewidth', 2)
    axis tight
    axis ij
    ylabel('Cell(#)')
    ylim([-4 331]);
    xlim([0 300]);
end
c = colorbar;
c.Label.String = 'zscore of df';

%% plot %%
data_nomedian = bsxfun(@(x, y)x-y, data, A_m);
data_z = bsxfun(@(x, y)x./y, data_nomedian, A_std);
[A2_m, A2_std, A2_sem] = median_std_sem(data_z, 2);

figure; 
% bg = - cumsum([0; A2_std*k_std]);
ind_choose = [1:10];
n_neu = length(ind_choose);
title_labels = {'to Pups', 'to Male', 'to Female'};
for sub =1:3
    subplot(1,3,sub); hold on;
    xoff_now = xoff(sub);
    for i=1:n_neu
        ii = ind_choose(i);
        data_now = data_z(ii, :)+ -(i-1)*10*1.5;
        h = plotHz(Fs, data_now, 'k');
        set(h, 'xdata', get(h, 'xdata') - xoff_now);
        MakeDrag(h, 'y');
    end
    barline(events{1}(:,1)-xoff_now, [-200 40], 'r', 'linewidth', 2)
    barline(events{2}(:,1)-xoff_now, [-200 40], 'g', 'linewidth', 2)
    barline(events{3}(:,1)-xoff_now, [-200 40], 'b', 'linewidth', 2)
    xlabel('Time (sec)');
    xlim([0 200]);
    set(gca, 'ytick',[-90 -80 -70 -60 -50 -40 -30 -20 -10 0]*1.5,'YTickLabel',...
    {'10','9','8','7','6','5','4','3','2','1',''});
    set(gca, 'tickdir', 'out');
    ylim([-70 30])
    scalebar('YLen', 10, 'XLen', nan)
    title(title_labels{sub});
    
end
subplot(1,3,1);
ylabel('Cell (#)')


%% neuron map %%
figure;
neuron.viewContours(neuron.Cn, 0.6, true, 1:5);
